<script setup lang="ts">
import { Delete, Edit } from '@element-plus/icons-vue'
import type { Action } from 'element-plus'
import { FormModal, SearchForm } from './components'
import { defaultFormData, defaultQuery } from './index'
import type {
  AuxiliaryInputInfo,
  AuxiliaryInputQueryListParams,
  AuxiliaryInputSaveOrUpdateParams,
  TableColumn,
} from '@/types'
import { auxiliaryInputApi } from '@/api'
// ------------------use------------------
// #region
const { globalProperties, $confirm, $message }
  = useCurrentInstance()
const { withLoading, loading } = useDefineLoading()
const [tableData, setTableData] = useState<
  AuxiliaryInputInfo[]
>([])
const [formData, setFormData] = useState<
  Partial<AuxiliaryInputSaveOrUpdateParams>
>({
  ...defaultFormData,
})
const [formModalTitle, setFormModalTitle]
  = useState<string>('')
const [newTarget, setNewTarget]
  = useState<AuxiliaryInputInfo | null>(null)
// #endregion
// ------------------静态变量------------------
// #region
const columns: TableColumn<AuxiliaryInputInfo>[] = [
  {
    key: uuid(),
    type: 'index',
    width: '80',
    label: '序号',
  },
  {
    key: uuid(),
    label: '时间',
    prop: 'dataTime',
  },
  {
    key: uuid(),
    label: '月产值(吨/万元)',
    prop: 'yield',
  },
  {
    key: uuid(),
    label: '月产量(吨)',
    prop: 'outputValue',
  },
  {
    key: uuid(),
    label: '综合能耗(吨标准煤)',
    prop: 'energyConsumption',
  },
  {
    key: uuid(),
    label: '单位产品能耗(吨标准煤)',
    prop: 'productUnitConsumption',
  },
  {
    key: uuid(),
    label: '单位产值能耗(吨标准煤)',
    prop: 'outputValueUnitConsumption',
  },
  {
    key: uuid(),
    label: '单位产品目标能耗(吨标准煤)',
    prop: 'productUnitConsumptionTarget',
  },
  {
    key: uuid(),
    label: '单位产值目标能耗(吨标准煤)',
    prop: 'outputValueUnitConsumptionTarget',
  },
]
// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const query = ref<Partial<AuxiliaryInputQueryListParams>>({
  ...defaultQuery,
})
const modalRef = ref<InstanceType<typeof FormModal>>()
// #endregion
// ------------------reactive------
// #region

// #endregion
// ------------------computed------
// #region
// #endregion
// ------------------生命周期------------------
// #region
onMounted(() => {
  getTableData()
  getNewTarget()
})
// #endregion
// ------------------内置方法---------------------
// #region

// #endregion
// ------------------方法---------------------
// #region
async function deleteData(id: number) {
  const res: Action = await $confirm(
    '该操作将永久删除该数据，确定删除？',
    '警告',
    {
      type: 'warning',
    },
  ).catch(e => e)

  if (res !== 'confirm')
    return
  await auxiliaryInputApi.deleteById(id)
  globalProperties.$message.success('删除成功！')
  getTableData()
}
async function showEditDialog(row: any) {
  modalRef.value?.open()
  await nextTick()
  setFormModalTitle('编辑')
  setFormData({
    ...defaultFormData,
    ...row,
    productUnitConsumptionTarget:
      row?.productUnitConsumptionTarget
      ?? newTarget.value?.productUnitConsumptionTarget,
    outputValueUnitConsumptionTarget:
      row?.outputValueUnitConsumption
      ?? newTarget.value?.outputValueUnitConsumption,
  })
}
async function showAddDialog() {
  modalRef.value?.open()
  await nextTick()
  setFormModalTitle('新增')
  setFormData({
    ...defaultFormData,
    productUnitConsumptionTarget:
      newTarget.value?.productUnitConsumptionTarget,
    outputValueUnitConsumptionTarget:
      newTarget.value?.outputValueUnitConsumption,
  })
}
async function getNewTarget() {
  const { data: res }
    = await auxiliaryInputApi.loadNewTarget()
  setNewTarget(res)
}
async function getTableData() {
  const { data: res } = await withLoading(
    auxiliaryInputApi.queryList,
  )(query.value)
  setTableData(res)
}
function success() {
  getTableData()
  modalRef.value?.close()
  $message.success('保存成功！')
}
// #endregion
</script>

<template>
  <div
    class="padding-sm flex-direction flex align-end container"
  >
    <view-table
      :loading="loading"
      :columns="columns"
      :data="tableData"
    >
      <template #actions>
        <SearchForm
          v-model="query"
          @search="getTableData"
          @reset="getTableData"
          @plus="showAddDialog"
        />
      </template>
      <template #operate="{ row }">
        <TdButton
          title="编辑"
          :icon="Edit"
          type="primary"
          @click="showEditDialog(row)"
        />
        <el-divider direction="vertical" />
        <TdButton
          title="删除"
          :icon="Delete"
          type="danger"
          @click="deleteData(row.id)"
        />
      </template>
    </view-table>

    <FormModal
      ref="modalRef"
      v-model:data="formData"
      :new-target="newTarget"
      :title="formModalTitle"
      @success="success"
    />
  </div>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
